package Lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class equalLock implements Runnable{

    private int tickets =10;
    private final Lock lock = new ReentrantLock(); // 定义一个Lock锁对象

    @Override
    public void run() {

        while (true){
            lock.lock(); // 对代码块进行加锁
            if (tickets>0){
                try {
                    Thread.sleep(100);
                    System.out.println(Thread.currentThread().getName()+"窗口正在出售第"+tickets--+"张票~");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock(); // 释放锁
                }
            }
        }
    }

    public static void main(String[] args) {

        equalLock e = new equalLock();
        new Thread(e,"1号").start();
        new Thread(e,"2号").start();
        new Thread(e,"3号").start();
        new Thread(e,"4号").start();

    }

}
